<template>
  <div>
    <el-button class="mb5" style="float: right" type="primary" @click="getList">
      <template #icon>
        <i-ep-search/>
      </template>
      刷新
    </el-button>
    <el-table v-adaptive="90" :border="true" :data="examApplyList" :header-cell-style="$headerCellStyle" :stripe="true"
              highlight-current-row>
      <el-table-column fixed="left" label="明细" type="expand">
        <template #default="props">
          <div m="4">
            <el-table :data="props.row.examItemVoList" :stripe="true" highlight-current-row>
              <el-table-column fixed="left" width="50px"/>
              <el-table-column fixed="left" label="项目代码" prop="itemCode" width="120px"/>
              <el-table-column fixed="left" label="项目名称" prop="itemName" width="160px"/>
              <el-table-column :show-overflow-tooltip="true" fixed="left" label="部位" prop="examPosition"
                               width="120px">
                <template #default="scope">
                  {{ scope.row.examPositionName }}
                </template>
              </el-table-column>
              <el-table-column :align="'center'" fixed="left" label="收费标志" prop="chargeFlag"
                               width="120">
                <template #default="scope">
                  <dict-data-tag :options="dictDataMap['charge_flag_dict']"
                                 :value="scope.row.chargeFlag"/>
                </template>
              </el-table-column>
              <el-table-column :align="'center'" fixed="left" label="确认标志" prop="confirmFlag"
                               width="120">
                <template #default="scope">
                  <dict-data-tag :options="dictDataMap['confirm_flag']"
                                 :value="scope.row.confirmFlag"/>
                </template>
              </el-table-column>
              <el-table-column fixed="left" label="数量" prop="num" width="80px"/>
              <el-table-column fixed="left" label="单价(元)" prop="price" width="110px"/>
              <el-table-column fixed="left" label="总价(元)" prop="costs" width="110px"/>
            </el-table>
          </div>
        </template>
      </el-table-column>
      <el-table-column :align="'center'" fixed="left" label="申请序号" prop="applyNo" width="120"/>
      <el-table-column :align="'center'" fixed="left" label="收费标志" prop="chargeFlag" width="120">
        <template #default="scope">
          <dict-data-tag :options="dictDataMap['charge_flag_dict']" :value="scope.row.chargeFlag"/>
        </template>
      </el-table-column>
      <el-table-column :align="'center'" fixed="left" label="确认标志" prop="confirmFlag" width="80">
        <template #default="scope">
          <dict-data-tag :options="dictDataMap['confirm_flag']" :value="scope.row.confirmFlag"/>
        </template>
      </el-table-column>
      <el-table-column :align="'center'" label="是否加急" prop="isUrgent">
        <template #default="scope">
          <dict-data-tag :options="dictDataMap['sys_yes_no']" :value="scope.row.isUrgent"/>
        </template>
      </el-table-column>
      <el-table-column :align="'center'" label="检查类别" prop="examTypeName"/>
      <el-table-column :align="'center'" :show-overflow-tooltip="true" label="症状" prop="symptom" width="120"/>
      <el-table-column :align="'center'" label="体征" prop="sign" width="120"/>
      <el-table-column :align="'center'" :show-overflow-tooltip="true" label="临床诊断" prop="clinicalDiag"
                       width="120"/>
      <el-table-column :align="'center'" label="开单时间" prop="realDate" width="130">
        <template #default="scope">
          <span>{{ $parseTime(scope.row.realDate, "{y}-{m}-{d} {h}:{i}:{s}") }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="其他诊断" :align="'center'" prop="relevantDiag" /> -->
      <el-table-column :align="'center'" label="确认人" prop="confirmUserName"/>
      <!-- <el-table-column label="取消确认" :align="'center'" prop="cancelConfirmUser" /> -->
      <el-table-column :align="'center'" label="确认时间" prop="confirmDate" width="80">
        <template #default="scope">
          <span>{{ $parseTime(scope.row.confirmDate, "{y}-{m}-{d} {h}:{i}:{s}") }}</span>
        </template>
      </el-table-column>
      <el-table-column :align="'center'" label="开单科室" prop="applyDeptName"/>
      <el-table-column :align="'center'" label="执行科室" prop="implementDeptName"/>
      <el-table-column :align="'center'" :show-overflow-tooltip="true" class-name="small-padding fixed-width"
                       fixed="right"
                       label="操作" width="120">
        <template #default="scope">
          <el-tooltip class="item" content="删除" effect="dark" placement="top">
            <el-button v-if="scope.row.confirmFlag === '1' && scope.row.chargeFlag === '1'" circle type="danger"
                       @click="handleDelete(scope.row)">
              <template #icon>
                <i-ep-delete/>
              </template>
            </el-button>
          </el-tooltip>
          <el-tooltip
            class="item"
            content="打印"
            effect="dark"
            placement="left"
          >
            <el-button circle type="primary" @click="printExamInfo(scope.row)">
              <template #icon>
                <i-ep-printer/>
              </template>
            </el-button>
          </el-tooltip>
          <el-tooltip class="item" content="查看结果" effect="dark" placement="top">
            <el-button circle
                       type="warning"
                       @click="queryViewReportFun(scope.row)"
            >
              <i-ep-view/>
            </el-button>
          </el-tooltip>
          <!--            <span v-else-if="scope.row.chargeFlag === '3'">-->
          <!--              <el-button-->
          <!--                type="danger"-->
          <!--                @click="refundApplicationFun(scope.row, '2', 'all')"-->
          <!--              >-->
          <!--                <svg-icon class="mr5" icon-class="refundApplication" />-->
          <!--                撤销退费(整单)-->
          <!--              </el-button>-->
          <!--            </span> -->
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageNum"
                :total="total" @pagination="getList"/>
  </div>

  <el-dialog v-model="open" :close-on-click-modal="false" :title="'查看检查结果'" append-to-body width="50%"
             @close="dialogClose">
    <div class="search-container mt20" style="height: 400px ;overflow-y: auto">
      <el-row>
        <el-form-item label="患者姓名：" label-width="100px">
          <span class="ys-css">{{ viewReport.patientOtherName }}</span>
        </el-form-item>
        <el-form-item label="患者性别：" label-width="222px">
          <span class="ys-css">{{ viewReport.patientSexBig }}</span>
        </el-form-item>
        <el-form-item label="患者年龄：" label-width="180px">
          <span class="ys-css">{{ viewReport.patientAge }}</span>
        </el-form-item>
        <el-form-item label="报告日期：" label-width="160px">
          <span class="ys-css">{{ viewReport.reportaprovedDate }}</span>
        </el-form-item>
        <el-row>
          <el-form-item label="检查号：" label-width="100px">
            <span class="ys-css">{{ viewReport.reportNumber }}</span>
          </el-form-item>
          <el-form-item label="诊断医生：" label-width="150px">
            <span class="ys-css">{{ viewReport.reqphysician }}</span>
          </el-form-item>
          <el-form-item label="设备：" label-width="150px">
            <span class="ys-css">{{ viewReport.modalityId }}</span>
          </el-form-item>
          <el-form-item label="部位：" label-width="174px">
            <span class="ys-css">{{ viewReport.bodyPart }}</span>
          </el-form-item>
        </el-row>
      </el-row>
      <el-row>
        <el-row>
          <el-form-item label="描述：" label-width="100px">
            <span class="ys-css">{{ viewReport.studyDescription }}</span>
          </el-form-item>
        </el-row>
        <el-form-item label="诊断结论：" label-width="100px">
          <span class="ys-css">{{ viewReport.reasonForStudy }}</span>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="url：" label-width="100px">
          <el-link type="warning" @click="openUrl(viewReport.url)">{{ viewReport.url }}</el-link>
          <span class="ys-css"></span>
        </el-form-item>
      </el-row>
    </div>
  </el-dialog>
</template>

<script setup lang="ts">
import {delExamApply, queryViewReport, refundApplication, selectPageExamApply} from "@/api/patient/examApply";
// import {queryViewReport} from "@/api/patient/pacsViewReport.ts"
import {useDictStoreHook} from "@/store/modules/dict";
// import {useUserStoreHook} from "@/store/modules/user";
// const useUserStore = useUserStoreHook();
// // 登录用户信息
// const userInfo = <UserInfoBusi>useUserStore.user.userInfo
const dictStore = useDictStoreHook();
let dictDataMap = ref(
  dictStore.isTypeGetData(
    "clinic_or_visit",
    "charge_flag_dict",
    "sys_yes_no",
    "position_type",
    "confirm_flag"
  )
);
const {proxy} = getCurrentInstance() as any;

const props = defineProps({
  inoroutFlag: String,
  orgId: String,
});
const queryParamsRef = ref();
const queryParams = ref<any>({
  pageNum: 1,
  pageSize: 10,
});
const examApplyList = ref([]);
const open = ref(false);
const total = ref(0);
const clinicId = ref(proxy.$clinicId);
const patientId = ref(proxy.$patientId);

/**
 * 检查结果
 */
const viewReport = ref<any>({})

function getList() {
  if (props.inoroutFlag === "4") {
    queryParams.value.clinicId = clinicId;
  }
  queryParams.value.inoroutFlag = props.inoroutFlag;
  queryParams.value.orgId = props.orgId;
  selectPageExamApply(queryParams.value).then((response: any) => {
    examApplyList.value = response.rows;
    total.value = response.total;
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const clinicIndexIds = row.id;
  ElMessageBox.confirm("是否确认删除？", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      delExamApply(clinicIndexIds).then((res: any) => {
        if (res.data === 1) {
          ElMessage.success("删除成功");
        } else if (res.data === 999) {
          ElMessage.error("检查已删除");
        } else if (res.data === 998) {
          ElMessage.error("检查已确认,不能删除");
        } else if (res.data === 992) {
          ElMessage.error("检查已收费,不能删除");
        } else if (res.data === 993) {
          ElMessage.error("检查已申请退费,不能删除");
        } else if (res.data === 994) {
          ElMessage.error("检查已退费,不能删除");
        } else if (res.data === 995) {
          ElMessage.error("检查已确认退费,不能删除");
        } else if (res.data === 996) {
          ElMessage.error("检查科室确认退费中,不能删除");
        } else {
          ElMessage.error("删除失败，请联系管理员。");
        }
        getList();
      });
    })
    .catch(() => {
    });
}

/**
 * 申请、撤销退费申请
 * type 申请类型  all 全部， single 单个
 */
function refundApplicationFun(row, chargeFlag, type) {
  let message = "确认要进行申请退费？";
  if (chargeFlag === "2") {
    // 撤销退费
    message = "确认要进行撤销退费操作吗？";
  }
  ElMessageBox.confirm(message, {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    refundApplication(row.id, chargeFlag, type).then((res: any) => {
      if (res.data) {
        if (chargeFlag === "2") {
          ElMessage.success("撤销退费成功。");
        } else if (chargeFlag === "3") {
          ElMessage.success("申请退费成功。");
        }
      } else {
        if (chargeFlag === "2") {
          ElMessage.success("撤销退费失败。");
        } else if (chargeFlag === "3") {
          ElMessage.error("申请退费失败。");
        }
      }
      getList();
    });
  });
}

// /** 单个项目退费、撤销退费申请 */
// function itemRefundApplicationFun(row, chargeFlag) {
//   let message = "确认要进行申请退费？";
//   if (chargeFlag === "2") {
//     // 撤销退费
//     message = "确认要进行撤销退费操作吗？";
//   }
//   ElMessageBox.confirm(message, {
//     confirmButtonText: "确定",
//     cancelButtonText: "取消",
//     type: "warning",
//   }).then(() => {
//     itemRefundApplication(row.id, chargeFlag).then((res: any) => {
//       if (res.data) {
//         if (chargeFlag === "2") {
//           ElMessage.success("撤销退费成功。");
//         } else if (chargeFlag === "3") {
//           ElMessage.success("申请退费成功。");
//         }
//       } else {
//         if (chargeFlag === "2") {
//           ElMessage.success("撤销退费失败。");
//         } else if (chargeFlag === "3") {
//           ElMessage.error("申请退费失败。");
//         }
//       }
//       getList();
//     });
//   });
// }

function queryViewReportFun(row: any) {
  const param = {
    applyNo: row.applyNo,
    id: row.id,
    orgId: row.orgId,
  }
  queryViewReport(param).then((res: any) => {
    if (!res.data) {
      return ElMessage.error("未查询到检查结果，请稍后再次查看")
    }
    open.value = true;
    viewReport.value = res.data;
  })
}

function dialogClose() {
  open.value = false;
}

function openUrl(url: string) {
  window.open(url, "_blank");
}

/**
 * 检查申请打印
 * @param row
 */
function printExamInfo (row: any) {
  let url = "/business/examApply/printExamInfo";
  const formData = new FormData()
  formData.append("id", row.id)
  formData.append("orgId", row.orgId)
  // pdf打印 、打印预览
  proxy.$printPdfPublic(url, formData).then(res => {
    if (res) {
      // objectUrl.value = URL.createObjectURL(res)
      // nextTick(() => {
      // printPreviewRef.value.init(objectUrl.value)
      // })
    } else {
      console.error('打印失败')
    }
  })
}

getList();
defineExpose({
  getList,
});
</script>
<script lang="ts">
export default {
  name: "applyTable",
};
</script>
